﻿using Microsoft.EntityFrameworkCore.Internal;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Linq;
using WebApiV3.Data;
using WebApiV3.Data.Entity;
using WebApiV3.Domain;

namespace WebApiV3.Utils
{
    public class DbInitialization
    {
        public static void Seeb(IServiceProvider serviceProvider)
        {

            using (var service = serviceProvider.CreateScope())
            {
                var db = service.ServiceProvider.GetService(typeof(Admin10086525DbContext)) as Admin10086525DbContext;
                var _users = service.ServiceProvider.GetService(typeof(IRespository<Users>)) as EfRespository<Users>;
                var _ranks = service.ServiceProvider.GetService(typeof(IRespository<Ranks>)) as EfRespository<Ranks>;

                db.Database.EnsureCreated();
                var temeRanks = db.Ranks.Any();
                if (!temeRanks)
                {
                    var ranks = new Ranks
                    {
                        RankName = "计应四班"
                    };
                    _ranks.Insert(ranks);

                    var users = new Users
                    {
                        UserName = "小俊",
                        Sex = "男",
                        Age = 20,
                        RanksId = ranks.Id
                    };
                    _users.Insert(users);
                }

            }

        }
    }
}
